package com.ruitu.mall_98.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.utils.AppUtils;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98.MainActivity;
import com.ruitu.mall_98.R;

public class StartActivity extends AppCompatActivity {
    // 下面的服务协议弹窗
    private RelativeLayout rl_service;
    private TextView tv_no, tv_ok, tv_serv_1;// 暂不使用,同意,内容

    private boolean isDestroy = false;//页面是否已经销毁

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_start);

        rl_service = findViewById(R.id.rl_service);
        tv_no = findViewById(R.id.tv_no);
        tv_ok = findViewById(R.id.tv_ok);
        tv_serv_1 = findViewById(R.id.tv_serv_1);

        handleServiceAlert();// 用户服务协议和隐私政策
    }

    // 服务协议和隐私政策
    private void handleServiceAlert() {
        boolean isShowServiceAlert = Arad.preferences.getBoolean("isShowServiceAlert", true);
        if (isShowServiceAlert) {
            if (rl_service.getVisibility() == View.GONE) {
                rl_service.setVisibility(View.VISIBLE);

                SpannableString ss = new SpannableString(getResources().getString(R.string.service));
                ClickableSpan cs1 = new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        Intent intent = new Intent(StartActivity.this, ServiceAndPrivacyActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("type", "1");
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                };
                ClickableSpan cs2 = new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        Intent intent = new Intent(StartActivity.this, ServiceAndPrivacyActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("type", "2");
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                };

                ss.setSpan(cs1, 86, 93, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                ss.setSpan(cs2, 94, 100, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                tv_serv_1.setText(ss);
                tv_serv_1.setMovementMethod(LinkMovementMethod.getInstance());

                tv_no.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ToastUtils.showShort("谢谢使用，有缘再见");
                        Arad.preferences.putBoolean("isShowServiceAlert", true).flush();
                        finish();
                    }
                });
                tv_ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rl_service.setVisibility(View.GONE);
                        // 同意不再弹窗
                        Arad.preferences.putBoolean("isShowServiceAlert", false).flush();
                        enterApp();
                    }
                });
                rl_service.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            }
        } else {
            enterApp();
        }
    }

    private void enterApp() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!isDestroy) {
//                    if (Arad.preferences.getBoolean("is_first_boot", true)) {//第一次启动,进入引导页面
//                        startGuidePage();
//                        Arad.preferences.putBoolean("is_first_boot", false).flush();//已经启动过引导页面,不是第一次启动了
//                    } else {//不是第一次启动,直接进入主页面
//                        startMainPage();
//                    }

                    int currVersion = AppUtils.getAppVersionCode();
                    if (Arad.preferences.getInteger("last_version", 0) < currVersion) {
                        //说明以前安装的版本没有当前的版本高,说明是刚安装的新版本
                        startGuidePage();
                        Arad.preferences.putInteger("last_version", currVersion).flush();//已经启动过引导页面
                    } else {
                        startMainPage();
                    }
                }
                finish();
            }
        }, 1600);
    }

    private void startMainPage() {//启动主页面
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    private void startGuidePage() {//启动引导页面
        Intent intent = new Intent(this, GuideActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isDestroy = true;
    }

}
